package com.eightywork.life.activity;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.adapter.ChooseAdapter;
import com.eightywork.life.model.Space;
import com.eightywork.life.service.CpService;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.eightywork.life.widget.MyGridView;
import com.umeng.analytics.MobclickAgent;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class FriendChooseActvity extends BaseActivity {
	private MyGridView publicGrid, areaGrid, liveGrid;
	private List<Space> publicList = new ArrayList<Space>();
	private List<Space> areaList = new ArrayList<Space>();
	private List<Space> liveList = new ArrayList<Space>();
	private ChooseAdapter publicAdapter, areaAdapter, liveAdapter;
	private ImageButton nextButton;
	private AsyncTask task, save;
	private List<Space> friends;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_friend_choose);
		init();
		getList();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		if (task != null) {
			task.cancel(true);
		}
		if (save != null) {
			save.cancel(true);
		}
		super.onDestroy();
	}

	public void init() {
		initHeader("好友推荐");
		findViewById(R.id.button_left).setVisibility(View.INVISIBLE);
		publicGrid = (MyGridView) findViewById(R.id.public_choose);
		areaGrid = (MyGridView) findViewById(R.id.area_choose);
		liveGrid = (MyGridView) findViewById(R.id.live_choose);
		publicAdapter = new ChooseAdapter(context, publicList);
		areaAdapter = new ChooseAdapter(context, areaList);
		liveAdapter = new ChooseAdapter(context, liveList);
		publicGrid.setAdapter(publicAdapter);
		areaGrid.setAdapter(areaAdapter);
		liveGrid.setAdapter(liveAdapter);
		nextButton = (ImageButton) findViewById(R.id.next_button);
		nextButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				save = new SaveTask().execute();
			}
		});
	}

	class SaveTask extends AsyncTask<Void, Void, String> {
		String applyuid = "";
		@Override
		protected void onPreExecute() {
			showProgressDialog("请稍候...");
			applyuid = "";
			for (String id : publicAdapter.getSelect()) {
				applyuid += id + "|";
			}
			for (String id : areaAdapter.getSelect()) {
				applyuid += id + "|";
			}
			for (String id : liveAdapter.getSelect()) {
				applyuid += id + "|";
			}
			Log.d("applyuid1", applyuid);
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... arg0) {
			try {
				CpService.friendMulAdd(applyuid,"");
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result == null) {
				Intent intent = new Intent(context, GroupChooseActivity.class);
				startActivity(intent);
				finish();
			} else {
				showMessage(result);
			}
			hideProgressDialog();
			super.onPostExecute(result);
		}
	}

	public void getList() {
		task = new MyTask().execute();
	}

	class MyTask extends AsyncTask<Void, Void, String> {
		@Override
		protected void onPreExecute() {
			showProgressDialog("请稍候...");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... arg0) {
			try {
				friends = SpaceService.recommendFriend();
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			hideProgressDialog();
			if (result == null) {
				for (Space space : friends) {
					switch (Integer.valueOf(space.getVerifyType())) {
					case 4:
						publicList.add(space);
						break;
					case 3:
						areaList.add(space);
						break;
					default:
						liveList.add(space);
						break;
					}
				}
				publicAdapter.notifyDataSetChanged();
				areaAdapter.notifyDataSetChanged();
				liveAdapter.notifyDataSetChanged();
			} else {
				showMessage(result);
			}
		}
	}
}
